﻿Imports System.Web.Mvc
Imports System.IO
Imports ADODBU

Public Interface iReportProc
    Function CreateReport(ByVal User As String, ByVal Target As String, ByVal param As String) As JsonResult
End Interface

Public MustInherit Class AbsReport
    Implements iReportProc


    Public MustOverride Function CreateReport(ByVal User As String, ByVal Target As String, ByVal param As String) As System.Web.Mvc.JsonResult Implements iReportProc.CreateReport


End Class

Public Class ReportHelper
    Shared mListReport As ListReport
    Shared Sub InitThis()
        If Dir(AppDomain.CurrentDomain.BaseDirectory & "Models\ReportList.txt") <> "" Then
            Dim sr As StreamReader = New StreamReader(AppDomain.CurrentDomain.BaseDirectory & "Models\ReportList.txt")
            Dim cdata As String
            Dim arr() As String
            mListReport = New ListReport
            Do While sr.Peek() >= 0
                cdata = sr.ReadLine()
                arr = cdata.Split(";")
                mListReport.AddItem(arr(0), New DataObj With {.FileName = arr(1), .ClassName = arr(2)})
            Loop
            sr.Close()
        End If


    End Sub

    Shared Function CreateReport(ByVal param As String) As JsonResult
        Dim x As DataObj
        If mListReport Is Nothing Then
            InitThis()
        End If

        Dim vd As New ReportParam
        RowClassUtil.RowClassFill(vd, param)

        x = mListReport.Item(vd.ModuleID)
        If Not x Is Nothing Then
            Dim xr As iReportProc
            xr = Geto(x)
            If xr Is Nothing Then
                Return Nothing
            Else
                Return xr.CreateReport(vd.UserID, vd.ModuleID, vd.Params)
            End If
        Else
            Return Nothing
        End If
    End Function

    Shared Function Geto(ByVal dta As DataObj) As iReportProc
        Dim currDomain As AppDomain
        Dim TblData As iReportProc
        Dim objHandle As System.Runtime.Remoting.ObjectHandle
        Try

            currDomain = AppDomain.CurrentDomain
            objHandle = currDomain.CreateInstanceFrom(AppDomain.CurrentDomain.BaseDirectory & "bin\" & dta.FileName, dta.ClassName)
            TblData = objHandle.Unwrap()
            Return TblData

        Catch e As Exception
            Return Nothing
        End Try
    End Function
End Class

Public Class ListReport
    Inherits System.Collections.Specialized.NameObjectCollectionBase
    Dim _DataObj As DataObj
    Public Sub AddItem(ByVal name As String, ByVal data As DataObj)
        Me.BaseAdd(name, data)
    End Sub
    ' Gets a key-and-value pair (DictionaryEntry) using an index.
    Default Public ReadOnly Property Item(ByVal index As Integer) As DataObj
        Get

            Return Me.BaseGet(index)
        End Get
    End Property
    ' Gets a key-and-value pair (DictionaryEntry) using an index.
    Default Public ReadOnly Property Item(ByVal index As String) As DataObj
        Get
            Try
                Return Me.BaseGet(index)
            Catch ex As Exception
                Return Nothing
            End Try

        End Get
    End Property
End Class

Public Class ReportParam
    Inherits ADODBU.RowClass
    Dim mtbl As String
    Dim mSource As String
    Dim mFile As String
    Dim _TableName As String
    Public Property UserID() As String
        Get
            Return _TableName
        End Get
        Set(ByVal value As String)
            _TableName = value
        End Set
    End Property
    Dim _FileName As String
    Public Property ModuleID() As String
        Get
            Return _FileName
        End Get
        Set(ByVal value As String)
            _FileName = value
        End Set
    End Property

    Dim _ObjName As String
    Public Property Params() As String
        Get
            Return _ObjName
        End Get
        Set(ByVal value As String)
            _ObjName = value
        End Set
    End Property
End Class

